<?php
/**
 * 注释不留名,代码随便用
 */

namespace app\common\traits;


use library\tools\Data;

trait SelectPage
{
    public function selectpage()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'htmlspecialchars']);
        //搜索关键词,客户端输入以空格分开,这里接收为数组
        $word = (array)$this->request->request("q_word/a");
        //当前页
        $page = $this->request->request("pageNumber");
        //分页大小
        $pagesize = $this->request->request("pageSize");
        //搜索条件
        $andor = $this->request->request("andOr", "and", "strtoupper");
        //排序方式
        $order = (array)$this->request->request("orderby/a");
        //显示的字段
        $field = $this->request->request("showField");
        //主键
        $searchKey = $this->request->request("searchKey");
        //主键值
        $searchValue = $this->request->request("searchValue");
        //搜索字段
        $searchfield = $this->request->request("searchField");
        $searchfield = $searchfield ? explode(',', $searchfield) : [];
        //自定义搜索条件
        $custom = (array)$this->request->request("custom/a");
        //是否返回树形结构
        $istree = $this->request->request("isrree", 0);
        $extend_where = $this->request->request('where');

        //默认值
        if ($istree) {
            $word = [];
            $pagesize = 99999;
        }

        //如果有primaryvalue,说明当前是初始化传值
        if ($searchValue !== null) {
            $where = [$searchKey => ['in', $searchValue]];
        } else {
            $where = function ($query) use ($word, $andor, $field, $searchfield, $custom) {
                $logic = $andor == 'AND' ? '&' : '|';
                $searchfield = is_array($searchfield) ? implode($logic, $searchfield) : $searchfield;

                foreach ($word as $k => $v) {
                    $query->where(str_replace(',', $logic, $searchfield), "like", "%{$v}%");
                }
                if ($custom && is_array($custom)) {
                    foreach ($custom as $k => $v) {
                        if (is_array($v) && 2 == count($v)) {
                            $query->where($k, trim($v[0]), $v[1]);
                        } else {
                            $query->where($k, '=', $v);
                        }
                    }
                }
            };
        }

        if(!empty($extend_where)){
             parse_str($extend_where,$extend);
        }else{
            $extend = [];
        }
        $list = [];
        $total = $this->model->where($where)->where($extend)->count();
        if ($total > 0) {
            $list = $this->model->where($where)
                ->where($extend)
                ->order($order)
                ->page($page, $pagesize)
                ->field($this->selectpageFields())
                ->fetchCollection()
                ->select()->toArray();
            foreach ($list as $index => &$item) {
                unset($item['password'], $item['salt']);
            }
            if ($istree) {
                $list = Data::arr2tree($list);
            }
        }
        //这里一定要返回有list这个字段,total是可选的,如果total<=list的数量,则会隐藏分页按钮
        $data = ['list' => $list, 'total' => $total];
        $this->success('', $data);
    }

    protected function selectpageFields()
    {
        return "*";
    }
}